package com.ad.pet.api;


import com.ad.pet.common.data.http.HttpCode;
import com.ad.pet.common.data.utils.SpUtil;
import com.ad.pet.common.execption.ApiException;
import com.ad.pet.common.structure.key.TokenKey;
import com.ad.pet.models.result.ResultBean;

import rx.Observable;


/**
 * Created by z on.
 * Des:
 * Date: 2017/6/27
 */

public class ResultCheck {


    public static <T> Observable<T> checkResponse(ResultBean<T> res) {
        if (HttpCode.SUCCESS.equals(res.getCode())) {
            return Observable.just(res.getResponseData());
        }else if ("10005".equals(res.getCode())||"10006".equals(res.getCode())||"10037".equals(res.getCode())){
            SpUtil.getInstance().putString(TokenKey.TOKEN,"");
        }
        return Observable.error(new ApiException(Integer.valueOf(res.getCode()),res.getMsg()));
    }
    //多个返回成功判断的
    public static <T> Observable<T> checkResponse2(ResultBean<T> res) {
        if (res.getCode().equals(HttpCode.SUCCESS)) {
            return Observable.just(res.getResponseData());
        }else if ("10005".equals(res.getCode())||"10006".equals(res.getCode())||"10037".equals(res.getCode())){
            SpUtil.getInstance().putString(TokenKey.TOKEN,"");
        }
        return Observable.error(new ApiException(res.getMsg()));
    }

    public static <T> Observable<T> checkResponseCode(ResultBean<T> res) {
        if (res.getCode().equals(HttpCode.SUCCESS)) {
            return Observable.just(res.getResponseData());
        }
        return Observable.error(new ApiException(Integer.valueOf(res.getCode()), res.getMsg()));
    }
    public static <T> Observable<T> checkLoginResponse(ResultBean<T> res) {
        if (res.getCode().equals(HttpCode.SUCCESS_LOGIN)) {
            return Observable.just(res.getResponseData());
        }
        return Observable.error(new ApiException(res.getMsg()));
    }
}
